﻿using HandyControl.Tools;
using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace MyMeeting.Common
{
   
    public class MenuPackIcon: PackIcon
    {
        [DllImport("user32")]
        public static extern IntPtr SetFocus(IntPtr hWnd);
        public ContextMenu Menu { get; set; }

        public MenuItem MenuItem { get; set; }
        bool isOutFoucs = false;
        System.Windows.Point pt;
        System.Windows.Point pos;
        protected override void OnMouseEnter(MouseEventArgs e)
        {
            base.OnMouseEnter(e);
         
            //if (this.Kind == PackIconKind.ChevronDown)
            //{
            //    this.Kind = PackIconKind.ChevronUp;
            //    this.Background = new SolidColorBrush(Color.FromRgb((byte)235, (byte)237, (byte)242));
            //}
            if (Menu != null)
            {
                Menu.PlacementTarget = this;
                isOutFoucs = true;
                Menu.IsOpen = true;
              //  SetFocus(this.GetHandle());
                pt = e.GetPosition(e.Device.Target);
                 pos = this.PointFromScreen(new System.Windows.Point(0, 0));

            }
            Debug.WriteLine("enter");
        }
        protected override void OnMouseLeave(MouseEventArgs e)
        {
            //Debug.WriteLine("OnMouseLeave");
            //if (Menu!=null&&Menu.IsOpen)
            //{
            //    return;
            //}
            //Debug.WriteLine("OnMouseLeave2");
            //base.OnMouseLeave(e);
            //if (this.Kind == PackIconKind.ChevronUp)
            //{
            //    this.Kind = PackIconKind.ChevronDown;
            //    this.Background = Brushes.Transparent;
            //}
        }
        //protected override void OnMouseMove(MouseEventArgs e)
        //{
        //    pt = e.GetPosition(e.Device.Target);
        //    base.OnMouseMove(e);
        //    Debug.Write($"{pt.X},{pt.Y},{this.ActualWidth},{this.ActualHeight}");
        //    Debug.WriteLine($"{pos.X},{pos.Y}");
        //}
        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);

            if (Menu != null)
            {
              //  Menu.StaysOpen = true;

                //Menu.LostMouseCapture += Menu_LostMouseCapture;
               // Menu.MouseLeave += Menu_MouseLeave;
                //Menu.Opened+= (s, ea) =>
                //{
                //    //this.Focus();
                  
                //   // Debug.Write($"{pt.X},{pt.Y},{this.ActualWidth},{this.ActualHeight}");
                //   // Debug.WriteLine($"{pos.X},{pos.Y}");
                //};
            }
        }

        //private void Menu_LostMouseCapture(object sender, MouseEventArgs e)
        //{
        //    Debug.WriteLine("Menu Live");
        //    var m = sender as ContextMenu;
        //    m.PlacementTarget = null;
        //    m.IsOpen = false;
        //    if (this.Kind == PackIconKind.ChevronUp)
        //    {
        //        this.Kind = PackIconKind.ChevronDown;
        //        this.Background = Brushes.Transparent;
        //    }
        //}

        //private void Menu_MouseLeave(object sender, MouseEventArgs e)
        //{
        //    Debug.WriteLine("Menu Live");
        //    var m = sender as ContextMenu;
        //    m.PlacementTarget = null;
        //    m.IsOpen = false;
        //    if (this.Kind == PackIconKind.ChevronUp)
        //    {
        //        this.Kind = PackIconKind.ChevronDown;
        //        this.Background = Brushes.Transparent;
        //    }

        //}
    
    }
}
